package com.nageoffer.shortlink.project.mq.producer;

import cn.hutool.core.lang.Snowflake;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

import java.util.Map;

import static com.nageoffer.shortlink.project.common.constant.RedisKeyConstant.SHORT_LINK_STATS_STREAM_TOPIC_KEY;

@Component
public class RMQShortLinkStatsSaveProducer {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Autowired
    private Snowflake snowflake;

    /**
     * producerMap.put("statsRecord", JSON.toJSONString(statsRecord));
     * @param producerMap
     */
    public void sendMessage(Map<String, String> producerMap) {
        //producerMap.put("statsRecord", JSON.toJSONString(statsRecord));
        String messageId = snowflake.nextIdStr();
        Message<Map<String, String>> message = MessageBuilder.withPayload(producerMap).setHeader("KEYS", messageId).build();
        String topic = "short-link-stats-save-topic";
        try{
            rocketMQTemplate.asyncSend(topic , message, null);
            System.out.println("消息发送成功，消息ID：" + messageId);
        }catch (Exception e){
            System.out.println("消息发送失败，消息ID：" + messageId);
        }
    }
}
